classdef EIGENVAL
  %EIGENVAL Summary of this class goes here
  %   Detailed explanation goes here
  
  properties
    Header;
    NumElectrons;
    NumKPoints;
    NumBands;
    KPoints;
    Energies;
  end
  
  methods
    function eObj = EIGENVAL(header, numElectrons, numKPoints, numBands,...
        kPoints, energies)
      eObj.Header = header;
      eObj.NumElectrons = numElectrons;
      eObj.NumKPoints = numKPoints;
      eObj.NumBands = numBands;
      eObj.KPoints = kPoints;
      eObj.Energies = energies;
    end
    
  end
  
  methods(Static)    
    
    test();
  
    function eObj = read(numFiles)
      if nargin == 0
        [header, numElectrons, numKpoints, numBands, kPoints, energiesArray] = EIGENVAL.readfile('EIGENVAL');
        eObj = EIGENVAL(header, numElectrons, numKpoints, numBands, kPoints, energiesArray);
      else
        eObjTemp = EIGENVAL.empty(numFiles, 0);
        for i = 1:numFiles
          [header, numElectrons, numKpoints, numBands, kPoints, energiesArray] = EIGENVAL.readfile(['EIGENVAL', num2str(i)]);
          eObjTemp(i) = EIGENVAL(header, numElectrons, numKpoints, numBands, kPoints, energiesArray);
        end

        numKPoints = sum([eObjTemp.NumKPoints]);
        kPoints = vertcat(eObjTemp.KPoints);      
        energiesArray = vertcat(eObjTemp.Energies);
        %kPointWeight = [eObjTemp.KPointsWeight];
        
        eObj = EIGENVAL(eObjTemp(1).Header, eObjTemp(1).Header,...
          numKPoints, eObjTemp(1).NumBands, kPoints, energiesArray);

      end
    end
    
    function [header, numElectrons, numKPoints, numBands, kPoints, energiesArray] = readfile(filename)
      fid = fopen(filename, 'r+t');
      ions = fscanf(fid, '%f\n', 4);
      volume = fscanf(fid, '%f\n', 5);
      T = fscanf(fid, '%f\n', 1);
      carString = fscanf(fid, '%s\n', 1);
      header = fscanf(fid, '%s\n', 1);
      numPoints = fscanf(fid, '%f\n', 3);
      numElectrons = numPoints(1);
      numKPoints = numPoints(2);
      numBands = numPoints(3);
      
      kPoints = zeros(numKPoints, 3);
      energiesArray = zeros(numKPoints, numBands);
      kPointWeight = zeros(numKPoints, 1);
      for i = 1:numKPoints
        kPoints(i, :) =  fscanf(fid, '%f\n', 3);
        kPointWeight(i, :) =  fscanf(fid, '%f\n', 1);
       
        energies = fscanf(fid, '%f\n', numBands*2);
        energies = reshape(energies, 2, numBands);
        energiesArray(i, :) = energies(2, :)';
      end
      fclose(fid);      
    end

  end
  
end

